﻿using PowerHub.MES.DTO.Shopfloor.QueryDTO;
using PowerHub.MES.EFBase;
using PowerHub.MES.Entities.Shopfloor;
using PowerHub.MES.Entities;
using PowerHub.MES.IRepository.IShopfloor;
using PowerHub.MES.ReturnEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PowerHub.MES.Entities.Crew;
using PowerHub.MES.IRepository.ICrew;
using Microsoft.EntityFrameworkCore;
using PowerHub.MES.DTO.Crew.QueryDto;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace PowerHub.MES.Repository.Crew
{
    /// <summary>
    ///排班计划数据实现层
    /// </summary>
    public class Crew_SchedulingPlanRepository : BaseRepository<Crew_SchedulingPlan>, ICrew_SchedulingPlanRepository
    {
        private PowerHubMesContext _repositorySys;
        public Crew_SchedulingPlanRepository(PowerHubMesContext repositorySys) : base(repositorySys)
        {
            _repositorySys = repositorySys;
        }


        /// <summary>
        /// 添加排班计划
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> CreateCrew_SchedulingPlanAsync(Crew_SchedulingPlan query)
        {
            var result = new OperationResult<bool>();
            result.Data = false;

            if (AddEntity(query) > 0)
            {
                result.Data = true;
            }

            return result;
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> DeletionCrew_SchedulingPlanAsync(List<string>? ids)
        {
            var result = new OperationResult<bool>();

            var Departments = GetEntitys().AsNoTracking().Where(p => ids.Contains(p.Id)).ToList();
            Departments.ForEach(w => w.IsDel = 1);
            result.Data = UpdataEntityRange(Departments) > 0 ? true : false;

            return result;
        }

        /// <summary>
        /// 获取排班计划列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationPageResult<List<Crew_SchedulingPlan>>> GetCrew_SchedulingPlanPageListAsync(QueryCrew_SchedulingPlanDTO query)
        {
            var result = new OperationPageResult<List<Crew_SchedulingPlan>>();

            var currentQuery = GetEntitys().AsNoTracking()
                                            .Include(p=>p.TeamBill)
                                            .Include(p=>p.ProductionLine)
                                            .Where(p => p.IsDel == 0).Where(p => p.SchedulingPlanName
                                            .Contains(query.SchedulingPlanName ?? string.Empty));

            result.PageIndex = query.PageIndex;
            result.PageSize = query.PageSize;
            result.TotalCount = await currentQuery.CountAsync();

            if (result.TotalCount > 0)
            {
                result.Data = await currentQuery.OrderByDescending(g => g.Createtime).Skip((query.PageIndex - 1) * query.PageSize).Take(query.PageSize).ToListAsync();
            }

            return result;
        }

        /// <summary>
        /// 更新排班计划
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> UpdateCrew_SchedulingPlanAsync(Crew_SchedulingPlan query)
        {
            var result = new OperationResult<bool>();
            result.Data = false;
            if (UpdataEntity(query) > 0)
            {
                result.Data = true;
            }
            return result;
        }

        /// <summary>
        /// 根据班组id获取排班计划
        /// </summary>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public async Task<Crew_SchedulingPlan> GetCrew_SchedulingPlanByIdAsync(QueryCrew_SchedulingPlanDTO pageQuery)
        {
            var currentQuery = GetEntitys().AsNoTracking()
                               .Where(s=>s.IsDel==0)
                               .Where(p => p.Id.Equals(pageQuery.SchedulingPlanId))
                               .Include(s=>s.TeamBill)
                               .Include(s=>s.ProductionLine)
                               .Include(s => s.CrewSchedulingPlanDetials)
                                   .ThenInclude(p => p.Userinfo)
                                     .ThenInclude(p => p.Department)
                               .Include(s=>s.CrewSchedulingPlanDetials)
                                   .ThenInclude(p => p.WorkStation)
                                        .ThenInclude(p => p.Process)
                               .FirstOrDefault();

            return currentQuery;
        }


        /// <summary>
        /// 根据计划Id绑定工单
        /// </summary>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public async Task<OperationResult<bool>> UpdateCrew_SchedulingPlanByBindWorkOrderAsync(Crew_SchedulingPlan query)
        {
            var result = new OperationResult<bool>();
            result.Data = false;
            if (UpdataEntity(query) > 0)
            {
                result.Data = true;
            }
            return result;
        }
    }
}
